//
//  SYComposeToolbarView.swift
//  SYWeibo
//
//  Created by Eric on 16/7/27.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit

enum SHComposeToolbarViewType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
    
}

class SYComposeToolbarView: UIView{
    
    //定义一个闭包
    var closure:((type: SHComposeToolbarViewType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置视图
    func setupUI(){
        backgroundColor =  RandomColor()
        //添加控件
        let  pictureButton = addChildButtom("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButtom("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtom("compose_trendbutton_background",type: .Trend)
        let emoticonButton = addChildButtom("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtom("compose_add_background",type: .Add)
        //设置约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        
        emoticonButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }

        
        
    }
    //监听方法
    func buttonClick(button: UIButton){
    
     closure?(type: SHComposeToolbarViewType(rawValue: button.tag)!)
        
    
    }
    
    
    //抽取创建 button的公共方法
    func addChildButtom(imgName: String, type: SHComposeToolbarViewType) ->UIButton {
        let button = UIButton()
        // rawValue 通过rawValue 把枚举名 转成枚举int数值
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
        
    }
    
    
}
